library(sf)
library(raster)
library(dplyr)
library(mapview)
library(RColorBrewer)
For this demonstration, we are using the cookfarm dataset from the GSIF package. This dataset is a list with 6 somponents storing various measurements that have been done on the farm.
data('cookfarm', package = "GSIF")
names(cookfarm)
## [1] "readings" "profiles" "bdensity" "grids" "weather"
## [6] "proj4string"
The profiles component stores soil profile data as a data.frame. We will turn this into a simple feature using the sf package:
profiles <- cookfarm$profiles %>%
st_as_sf(
coords = c('Easting', 'Northing'),
crs = cookfarm$proj4string
)
# simple plot using the sf package
plot(profiles)
The grids component is storing gridded data. We will reansform these into a RasterStack using the rasterFromXYZ function. This function requires the x and y columns to be the first column of the data.frame (from the left) – a good opportunity to use the select function from the dplyr package:
grids <- cookfarm$grids %>%
dplyr::select(x, y, DEM, TWI, Cook_fall_ECa, Cook_spr_ECa) %>% # Re-order the columns, and select 4 interesting variables
rasterFromXYZ(crs = cookfarm$proj4string) # The coordinate reference system is stored as one of the component in cookfarm
# simple plot using the raster package
plot(grids)
mapviewThe mapview package is basically a wrapper around the leaflet package.
It makes is very easy to create a leaflet map:
# couldn't be easier to create a map!
mapview(profiles)
But while the mapview function can be called just as is, there’s numerous details that can be tweaked. For example the colours:
# Create palettes
pal_continuous <- colorRampPalette(brewer.pal(7, "BrBG")) # For continuous data
pal_categorical <- colorRampPalette(brewer.pal(9, "Set1")) # For categorical data
# Pass the palette to mapview
mapview(
profiles,
zcol = "TAXSUSDA",
col.regions = pal_categorical,
legend = TRUE
)
# Tweaking backgrounds
mapview(profiles, zcol = "BLD", col.regions = pal_continuous, legend = TRUE, map.types = "Esri.WorldImagery")
# Burst to separate soil classes
mapview(profiles, zcol = "TAXSUSDA", col.regions = pal_categorical, legend = TRUE, burst = TRUE)
# Plot grids
mapview(grids)
mapview(grids, col.regions = pal_continuous, na.color = "transparent", legend = TRUE)
mapview(grids$TWI, col.regions = pal_continuous, na.color = "transparent", legend = TRUE)
# Raster specific functions
viewRGB(poppendorf)
viewRGB(poppendorf, 4, 3, 2)
mapviewOptions(
basemaps = c("Esri.WorldImagery", "Thunderforest.Landscape"),
na.color = "transparent"
)
mapview(profiles, zcol = "BLD") + mapview(grids$DEM)
mapviews together# Plot both together
mapview(grids$DEM, col.regions = pal_continuous, legend = TRUE) + mapview(profiles, zcol = "TAXSUSDA", col.regions = pal_categorical)
plainview(grids$DEM)
# Syncing several maps
m1 <- mapview(grids$DEM)
m2 <- mapview(grids$TWI)
m3 <- mapview(grids$Cook_fall_ECa)
sync(m1, m2, m3, ncol = 2, sync.cursor = TRUE)
This is an interactive analogue to the panelled graphs provided by ggplot2 or lattice.
img1 <- poppendorf[[1]]
img2 <- poppendorf[[5]]
slideview(
img1,
img2,
label1 = "Poppendorf-Layer-1",
label2 = "Poppendorf-Layer-2",
legend = TRUE
)
mapview(
profiles,
popup = popupImage('https://www.vcard.wur.nl/WebServices/GetMedia.ashx?id=37263')
)
mapviewlibrary(xts)
library(dygraphs)
profiles$SOURCEID <- as.character(profiles$SOURCEID)
records <- cookfarm$readings
records$SOURCEID <- as.character(records$SOURCEID)
ids <- unique(records$SOURCEID)
# Subset sensors
ids <- sample(ids, size = 5)
idx_sensors <- which(profiles$SOURCEID %in% ids)
sensors <- profiles[idx_sensors,]
make_ts <- function(id) {
records %>%
filter(SOURCEID == id) %>%
dplyr::select(-SOURCEID) %>%
dplyr::select(Date, ends_with('VW')) %>%
xts(.$Date)
}
make_dygraph <- function(id){
ts <- make_ts(id)
dygraph(ts)
}
l_graphs <- lapply(
ids,
make_dygraph
)
make_dygraph(ids[1])
mapview(sensors, popup = popupGraph(graphs = l_graphs, width = 300, height = 300))